<template>
  <van-overlay :show="show" @click="show = false">
    <div class="wrapper" @click.stop>
      <div class="block">
        <p class="title">选择你的学习阶段</p>
        <ul class="options">
          <li
            v-for="(item, index) of stageList"
            :key="item.gradeName"
            :class="{ active: item.gradeName === active }"
            @click="onItemClick(item, index)"
          >
            <span>{{ item.gradeName }}</span>
          </li>
        </ul>
        <span class="prompt">随时可以更改,请放心选择</span>
      </div>
    </div>
  </van-overlay>
</template>

<script lang="ts" setup>
import { ref, defineProps, computed, defineEmits, onMounted } from 'vue'
import axios from '../api/axios'

const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
  active: {
    type: String,
    default: '',
  },
})

const emit = defineEmits(['update:visible', 'update:active', 'onItemClick'])

const show = computed({
  get() {
    return props.visible
  },
  set(val) {
    emit('update:visible', val)
  },
})

const { stageList } = useStageList()

function onItemClick(item: { gradeName: string; id: string }, index: number) {
  emit('update:active', item.id)
  emit('onItemClick', item, index)
}

function useStageList() {
  const stageList = ref<any[]>([
    {
      gradeName: '1-3年级',
      id: '1,2,3',
    },
    {
      gradeName: '4-6年级',
      id: '4,5,6',
    },
    {
      gradeName: '初中',
      id: '7,8,9',
    },
    {
      gradeName: '高中',
      id: '10,11,12',
    },
  ])

  const url = '/api/blade-system/grade/list'

  const getStageList = async () => {
    const { data } = await axios.get(url)
    stageList.value = data.data
  }

  return {
    stageList,
  }
}
</script>

<style lang="less" scoped>
.wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
}

.block {
  width: 325px;
  height: 350px;
  background: #ffffff;
  border-radius: 10px;
  text-align: center;
  padding: 20px 50px;

  .title {
    margin: 12px;
    margin-bottom: 46px;
    height: 20px;
    font-size: 21px;
    font-family: PingFang SC, PingFang SC-Bold;
    font-weight: 700;
    color: #0387cf;
    line-height: 24px;
  }
  .prompt {
    font-size: 10px;
    color: #999;
    margin-bottom: 12px;
  }

  .options {
    margin-top: 20px;
    font-size: 15px;
    font-family: PingFang SC, PingFang SC-Regular;

    li {
      width: 100%;
      height: 30px;
      line-height: 30px;
      color: #505050;
      background: #dcdcdc;
      border-radius: 5px;
      margin-bottom: 7px;
      &.active {
        background: #3071b9;
        color: #fff;
      }
    }
  }
}
</style>
